<?php

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$_MAMBOTS->registerFunction( 'onLog', 'RecordDocmanHit' );

function RecordDocmanHit( $params ) {
    global  $my, $database, $mosConfig_absolute_path, $_MAMBOTS;

    $query = "SELECT * FROM #__comprofiler WHERE id = ".$my->id;
    $database->setQuery($query);
    $user = $database->loadObjectList();
    $user = $user[0];

    $user->name     = $my->name;
    $user->email    = $my->email;
    $user->username = $my->username;
    
    if ( !isset($_MAMBOTS->_content_mambot_params['botlogs']) ) {

        $query = "SELECT params"
        . "\n FROM #__mambots"
        . "\n WHERE element = 'botlogs'"
        . "\n AND folder = 'docman'"
        ;
        $database->setQuery( $query );
        $database->loadObject($mambot);
        
        $_MAMBOTS->_content_mambot_params['botlogs'] = $mambot;
    }

    $mambot = $_MAMBOTS->_content_mambot_params['botlogs'];
    
    $botParams = new mosParameters( $mambot->params );

    $extra1 = $botParams->get('extra1','');
    $extra2 = $botParams->get('extra2','');
    $extra3 = $botParams->get('extra3','');
    $extra4 = $botParams->get('extra4','');
    $extra5 = $botParams->get('extra5','');
    $extra6 = $botParams->get('extra6','');
    $extra7 = $botParams->get('extra7','');
    $extra8 = $botParams->get('extra8','');
    $extra9 = $botParams->get('extra9','');
    
    $params_path = $mosConfig_absolute_path . '/administrator/components/com_docman/docman.params.xml';
    if(file_exists($params_path)) {
        $parameters =& new dmParameters( $params['document']->attribs, $params_path , 'params' );
    }
    $parameters->render();
    $object = $parameters->toObject();

    if(!isset($_SESSION))  session_start();
    $query = "INSERT INTO #__docman_extended_logs VALUES (null, {$params['document']->id},'"
            .date('Y-m-d H:i:s')."',{$my->id}, {$object->ammount},"
            ."'".$database->getEscaped($object->question1)."','".$database->getEscaped(mosGetParam($_SESSION,'docmanquestion1'))."',"
            ."'".$database->getEscaped($object->question2)."','".$database->getEscaped(mosGetParam($_SESSION,'docmanquestion2'))."',"
            ."'".$database->getEscaped($object->question3)."','".$database->getEscaped(mosGetParam($_SESSION,'docmanquestion3'))."'"
            .($extra1 && isset($user->$extra1)?",'".$database->getEscaped($user->$extra1)."'":",''")
            .($extra2 && isset($user->$extra2)?",'".$database->getEscaped($user->$extra2)."'":",''")
            .($extra3 && isset($user->$extra3)?",'".$database->getEscaped($user->$extra3)."'":",''")
            .($extra4 && isset($user->$extra4)?",'".$database->getEscaped($user->$extra4)."'":",''")
            .($extra5 && isset($user->$extra5)?",'".$database->getEscaped($user->$extra5)."'":",''")
            .($extra6 && isset($user->$extra6)?",'".$database->getEscaped($user->$extra6)."'":",''")
            .($extra7 && isset($user->$extra7)?",'".$database->getEscaped($user->$extra7)."'":",''")
            .($extra8 && isset($user->$extra8)?",'".$database->getEscaped($user->$extra8)."'":",''")
            .($extra9 && isset($user->$extra9)?",'".$database->getEscaped($user->$extra9)."'":",''")
            .")";
    $database->setQuery($query);
    $database->query();

}

?>
